package com.ymh.git.thread.juc.goods;

import com.ymh.git.thread.juc.SleepTools;

/**
 * Created by aivin on 2018/11/6.
 * <p>
 * 使用内置锁来实现商品服务接口
 */
public class UseSync implements GoodsService {

    private GoodsInfo goodsInfo;

    public UseSync(GoodsInfo goodsInfo) {
        this.goodsInfo = goodsInfo;
    }

    /**
     *
     * @return
     */
    @Override
    public synchronized GoodsInfo getNum() {
        SleepTools.ms(5); //让线程休眠5毫秒
        return this.goodsInfo;
    }

    @Override
    public synchronized void setNum(int num) {
        SleepTools.ms(5);
        goodsInfo.changeNumber(num);
    }
}
